package com.cc.service.generate;


import cn.hutool.core.collection.CollectionUtil;
import com.cc.build.BuiderDao;
import com.cc.config.PackagePathConfiguration;
import com.cc.config.TemplateConfiguration;
import com.cc.pojo.file.BuiderAbstract;
import com.cc.pojo.file.FilePathAndClassName;
import com.cc.pojo.tablestruct.LibaryProperties;
import com.cc.pojo.tablestruct.NeedQueryParams;
import com.cc.service.core.GenerateAbstract;
import com.cc.utils.beanUtil.BeanUtils;
import com.cc.utils.ConvertHumpUnderlinesToEachOtherUtil;
import com.cc.utils.CoreUtil;


import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * TODO 不同类型应该有不同是查询条件，暂时没做出来
 *
 * @author c.c
 * @Description 核心生成类
 * @since 2023/3/30
 */

public class GenerateDao extends GenerateAbstract {
  private PackagePathConfiguration packagePathConfiguration = BeanUtils.getBean(PackagePathConfiguration.class);
  private TemplateConfiguration templateConfiguration = BeanUtils.getBean(TemplateConfiguration.class);

  public GenerateDao(List<NeedQueryParams> needQueryParams) {
    this.getTargetPackageAddress(needQueryParams);
  }

  public GenerateDao(Map<String, List<NeedQueryParams>> needQuerymaps) {
    this.getTargetPackageAddress(needQuerymaps);
  }

  @Override
  public List<FilePathAndClassName<BuiderAbstract>> getTargetPackageAddress(Object... youParameter) {
    Map<String, List<NeedQueryParams>> needQueryMaps = null;
    for (int i = 0; i < youParameter.length; i++) {
      needQueryMaps = (Map<String, List<NeedQueryParams>>) youParameter[0];
    }

    List<FilePathAndClassName<BuiderAbstract>> packageAddress = new ArrayList<>();
    List<LibaryProperties> libaryProperties = super.getLibaryProperties();
    for (LibaryProperties libaryProperty : libaryProperties) {
      if (CollectionUtil.isNotEmpty(libaryProperty.getTableStructs())) {
        StringBuilder packgePath = new StringBuilder();
        String path = CoreUtil.getPath(libaryProperty, packgePath, packagePathConfiguration.isDaoisLastLayer(), packagePathConfiguration.getDaoPath());
        //获取db名
        String classnameForDo = ConvertHumpUnderlinesToEachOtherUtil.underlineToHump(libaryProperty.getTableName(), true) + "Do";
        //获取dao名
        String classnameForDao = ConvertHumpUnderlinesToEachOtherUtil.underlineToHump(libaryProperty.getTableName(), true) + "Dao";

        String doPath = super.getPathAddress(classnameForDo);
        List<String> paths = Collections.singletonList(doPath);
        //获取形参变量
        List<NeedQueryParams> needQueryParams = null;
        if (needQueryMaps != null) {
          needQueryParams = needQueryMaps.get(libaryProperty.getTableName());
        }
        BuiderDao builderInter = new BuiderDao(packgePath.toString(), paths, classnameForDao, classnameForDo, needQueryParams);
        FilePathAndClassName<BuiderAbstract> filePathAndClassName = new FilePathAndClassName<>(path, classnameForDao, builderInter);
        super.putPathAddress(classnameForDao, packgePath);
        packageAddress.add(filePathAndClassName);
      }
    }
    super.build(packageAddress, templateConfiguration.getDaoPath());
    return packageAddress;
  }
}
